package com.example.onedemotest.slice;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class PageSliderProvider extends ohos.agp.components.PageSliderProvider {

    private ArrayList<DataItem> mDatas;
    private Context mContext;

    public PageSliderProvider(ArrayList<DataItem> mDatas,Context context) {
        this.mDatas = mDatas;
        this.mContext=context;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        DataItem dataItem = mDatas.get(i);
        Text label=new Text(mContext);
        label.setText(dataItem.mText);
        label.setTextSize(100);
        label.setTextColor(Color.BLACK);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setLayoutConfig(new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));

        //设置背景
        ShapeElement shapeElement=new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#AFEEEE")));
        label.setBackground(shapeElement);
        componentContainer.addComponent(label);
        return componentContainer;
    }

    //销毁容器中的指定页面
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

    static class DataItem{
        private String mText;

        public DataItem(String mText) {
            this.mText = mText;
        }
    }
}
